//
//  ATADJgSDKBiddingDelegate.m
//  AnyThinkSDKDemo
//
//  Created by 刘娟 on 2024/9/25.
//  Copyright © 2024 抽筋的灯. All rights reserved.
//

#import "ATADJgSDKBiddingDelegate.h"

#import "ATADJgSDKBiddingManager.h"
#import "ATADJgSDKBiddingRequest.h"

#import <ADJgSDK/ADJgSDK.h>
#import <ADJgSDK/ADJgSDKSplashAd.h>
#import <ADJgSDK/ADJgSDKRewardvodAd.h>
#import <AnyThinkSplash/AnyThinkSplash.h>
//开屏容器
#import "ADJgSDKSplashAdContainer.h"
//插屏容器
#import "ADJgSDKInterstitialAdContainer.h"
//激励视频容器
#import "ADJgSDKRewardVodAdContainer.h"
//信息流容器
#import "ADJgSDKNativeAdContainer.h"
//横幅容器
#import "ADJgSDKBannerAdContainer.h"
//注意：根据需要删除未使用到的容器和容器相应代理即可
@interface ATADJgSDKBiddingDelegate () <ADJgSDKSplashAdContainerDelegate,ADJgSDKInterstitialAdContainerDelegate,ADJgSDKRewardVodAdContainerDelegate,ADJgSDKNativeAdContainerDelegate,ADJgSDKBannerAdContainerDelegate>

@end

@implementation ATADJgSDKBiddingDelegate
- (void)jgsdk_loadSuccess:(id)ad withPrice:(NSInteger)bidPrice{
    ATADJgSDKBiddingRequest *request = [[ATADJgSDKBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {

        NSString *price = @(bidPrice/100.0f).stringValue;
#if DEBUG
        NSLog(@"ADJgSDK - 竞价价格：%@,广告位：%@",price,request.placementID);
#endif
        //注意：slot_id为后台配置的广告源key
        
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:price currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:ad];
        request.bidCompletion(bidInfo, nil);
    }
    
    [[ATADJgSDKBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}
- (void)jgsdk_loadFailLoadError:(NSError *)error{
    ATADJgSDKBiddingRequest *request = [[ATADJgSDKBiddingManager sharedInstance] getRequestItemWithUnitID:self.unitID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[ATADJgSDKBiddingManager sharedInstance] removeBiddingDelegateWithUnitID:self.unitID];
}
#pragma mark -- ADJgSDKSplashAdContainer
/**
 *  开屏广告素材加载成功
 */
- (void)jgsdkContainer_jgsdkSplashAdDidLoad:(ADJgSDKSplashAdContainer *)splashAd withPrice:(NSInteger)bidPrice {
    NSLog(@"%s", __FUNCTION__);
    [self jgsdk_loadSuccess:splashAd withPrice:bidPrice];
    
}

/**
 *  开屏广告请求失败
 */
- (void)jgsdkContainer_jgsdkSplashAdFailLoadError:(NSError *)error{
    NSLog(@"%s %@", __FUNCTION__ ,error);
    [self jgsdk_loadFailLoadError:error];
}
#pragma mark -- ADJgSDKInterstitialAdContainerDelegate
/**
 *  插屏广告素材加载成功
 */
- (void)jgsdkContainer_jgsdkInterstitialAdDidLoad:(ADJgSDKInterstitialAdContainer *)InterstitialAd withPrice:(NSInteger)bidPrice {
    [self jgsdk_loadSuccess:InterstitialAd withPrice:bidPrice];
}

/**
 *  插屏广告请求失败
 */
- (void)jgsdkContainer_jgsdkInterstitialAdFailLoadError:(NSError *)error {
    [self jgsdk_loadFailLoadError:error];
}

#pragma mark -- ADJgSDKRewardVodAdContainerDelegate
- (void)jgsdkContainer_jgsdkRewardVodAdDidLoad:(ADJgSDKRewardVodAdContainer *)RewardVodAd withPrice:(NSInteger)bidPrice {
    [self jgsdk_loadSuccess:RewardVodAd withPrice:bidPrice];
}

- (void)jgsdkContainer_jgsdkRewardVodAdFailLoadError:(NSError *)error {
    [self jgsdk_loadFailLoadError:error];
}

#pragma mark -- ADJgSDKNativeAdContainerDelegate
- (void)jgsdkContainer_jgsdkNativeAdDidLoad:(nonnull ADJgSDKNativeAdContainer *)NativeAd withPrice:(NSInteger)bidPrice {
    [self jgsdk_loadSuccess:NativeAd withPrice:bidPrice];
}

- (void)jgsdkContainer_jgsdkNativeAdFailLoadError:(nonnull NSError *)error {
    [self jgsdk_loadFailLoadError:error];
}

#pragma mark -- ADJgSDKBannerAdContainerDelegate
- (void)jgsdkContainer_jgsdkBannerAdDidLoad:(ADJgSDKBannerAdContainer *)bannerAd withPrice:(NSInteger)bidPrice {
    [self jgsdk_loadSuccess:bannerAd withPrice:bidPrice];
}

- (void)jgsdkContainer_jgsdkBannerAdFailLoadError:(NSError *)error {
    [self jgsdk_loadFailLoadError:error];
}
@end
